package com.panshi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panshi.entity.AppMatter;
import com.panshi.entity.AppMatterCheck;
import com.panshi.mapper.MatterCheckMapper;
import com.panshi.service.MatterCheckService;
import com.panshi.service.MatterService;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MatterCheckServiceImpl implements MatterCheckService {

    @Autowired
    private MatterCheckMapper matterCheckMapper;

    @Autowired
    private MatterService matterService;

    // 上架状态
    private String MATTER_GROUD = "1";
    // 下架状态
    private String MATTER_DOWN = "4";

    @Override
    public DataResult addMatter(AppMatterCheck appMatterCheck) {
        DataResult result = new DataResult();
        appMatterCheck.setMatterStatus("2");
        matterCheckMapper.addMatter(appMatterCheck);
        if(null != appMatterCheck.getMatterId()){
            result.setCode(200).setMsg("新增成功~").setData(appMatterCheck.getMatterId());
        }
        return result;
    }

    @Override
    public DataResult adoptCheck(AppMatterCheck appMatterCheck) {
        DataResult result = new DataResult();
        matterCheckMapper.adoptCheck(appMatterCheck);
        if(MATTER_GROUD.equals(appMatterCheck.getMatterStatus())){ // 上架
            AppMatterCheck app = matterCheckMapper.queryInfo(appMatterCheck.getMatterId());
            if(null != app){
                AppMatter data = matterService.queryInfo(app.getMatterId());
                if(null == data){
                    // 同步到问题表
                    matterService.addMatter(app);
                    // 增加搜索表

                    // 增加动态表
                }
            }
        }else if(MATTER_DOWN.equals(appMatterCheck.getMatterStatus())){ //下架
            AppMatter data = matterService.queryInfo(appMatterCheck.getMatterId());
            if(null != data){
                // 删除问题表
                matterService.deleteMatter(appMatterCheck.getMatterId());
            }

        }
        return result.setCode(appMatterCheck.getMatterId()).setMsg("执行成功~");
    }

    @Override
    public AppMatterCheck queryInfo(Integer matterId) {
        AppMatterCheck app = matterCheckMapper.queryInfo(matterId);

        AppMatter matter = matterService.queryInfo(app.getMatterId());
        if( app.getMatterId().equals(matter.getMatterId())){
            app.setReadCount(matter.getReadCount());
            app.setAnswerCount(matter.getAnswerCount());
            app.setCollecCount(matter.getCollectCount());
        }
        return app;
    }

    @Override
    public DataResult deleteMatter(Integer matterId) {
        DataResult result = new DataResult();
        AppMatterCheck app = matterCheckMapper.queryInfo(matterId);
        if(MATTER_DOWN.equals(app.getMatterStatus())){
            return result.setCode(matterId).setMsg("该数据已删除~");
        }else{
            // 逻辑删除审核表
            matterCheckMapper.deleteMatter(matterId);

            AppMatter data = matterService.queryInfo(matterId);
            if(null != data){
                // 删除问题表
                matterService.deleteMatter(matterId);
            }
        }

        return result.setCode(matterId).setMsg("删除成功~");
    }

    @Override
    public DataResult updateMatter(AppMatterCheck appMatterCheck) {
        DataResult result = new DataResult();
        AppMatterCheck app = matterCheckMapper.queryInfo(appMatterCheck.getMatterId());
        if(null != app){
            matterCheckMapper.updateMatter(appMatterCheck);
            // 修改时不是上架状态，需要重新审核
            if(!MATTER_GROUD.equals(app.getMatterStatus())){
                return  result.setCode(app.getMatterId()).setMsg("修改成功请等待审核~");
            }
            // 同步到问题表
            matterService.addMatter(app);
        }
        return result.setCode(app.getMatterId()).setMsg("修改成功~");
    }

    @Override
    public DataResult queryByUid(Integer userId, PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<AppMatterCheck> app = matterCheckMapper.queryByUid(userId);
        for(AppMatterCheck a: app){
            AppMatter  matter = matterService.queryInfo(a.getMatterId());
            if(null != matter && a.getMatterId().equals(matter.getMatterId())){
                a.setReadCount(matter.getReadCount());
                a.setAnswerCount(matter.getAnswerCount());
                a.setCollecCount(matter.getCollectCount());
            }
        }
        PageInfo pageInfo = new PageInfo(app);
        return new DataResult().setCode(200).setData(pageInfo);
    }

    @Override
    public DataResult query(Integer classifyId,PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<AppMatterCheck> app = matterCheckMapper.query(classifyId);
        PageInfo pageInfo = new PageInfo(app);
        return new DataResult().setCode(0).setCount(1000).setData(pageInfo);
    }

    @Override
    public DataResult queryUnCheck(PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<AppMatterCheck> app = matterCheckMapper.queryUnCheck();
        PageInfo pageInfo = new PageInfo(app);
        return new DataResult().setCode(0).setCount(1000).setData(pageInfo);
    }


}
